let domain = require('../domain/lifecycleDomain');

let userDao = require('../../../dao/userDao');
let userInfoServices = require('../../../services/userInfoServices');
let dispatch = require('../../../util/dispatcher');
let rpcAPI = require('../../../API/rpcAPI');
let code = require('../../../constant/code')
let logger = require('pomelo-logger').getLogger("robot");
let robotServices = require('../../../services/robotServices');
let commonDao = require('../../../dao/commonDao');
let unionManager = require('../../game/domain/unionManager');

module.exports = function (app) {
    return new remote(app);
};

let remote = function (app) {
    this.app = app;
};
let pro = remote.prototype;

pro.getRandomRobot = async function (unionID, roomID, cb) {

    let userData = await robotServices.getRandomRobot(unionID);

    if (userData && !!roomID) {
        let gameServer = dispatch.dispatch(roomID, this.app.getServersByType('game'));
        let isIn = await rpcAPI.isUserInRoom(gameServer.id, userData.uid, roomID);
        if (!isIn) {
            let resCode = await rpcAPI.joinRoom(gameServer.id, userData, roomID);
            if (resCode == 0) {
                logger.info(`[robot] Join Game Success  robotUserID = ${userData.uid} RoomId = ${roomID} unionID = ${unionID} `);
            } else {
                logger.error(`[robot] Join Game Fail RoomId = ${roomID} unionID = ${unionID} Code = ${resCode} errMsg = ${code[resCode]}`);
            }
        } else {
            logger.info(`[robot] User In Room unionID = ${unionID}  RoomId = ${roomID} robotUserID =  ${userData.uid} `);
        }

    }

};



